azogue/eventsensor

Multiple, nested events for sensor-state throw "bad_state"

Closed this issue · 1 comments

Hi,

it's me again ;-)

I've got this event:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.xyz_action",
        "old_state": {
            "entity_id": "sensor.xyz_action",
            "state": "wakeup",
            "attributes": {
                "action": "",
                "angle": -28.89,
                "battery": 74,
                "linkquality": 65,
                "side": 0,
                "voltage": 2955,
                "friendly_name": "home/attic/Fabian/Cube1_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-01-09T21:19:01.345668+00:00",
            "last_updated": "2021-01-09T21:19:01.347388+00:00",
            "context": {
                "id": "b4249243b8b5f553a7d9219423e5c92d",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.xyz_action",
            "state": "",
            "attributes": {
                "action": "",
                "angle": -28.89,
                "battery": 74,
                "linkquality": 65,
                "side": 0,
                "voltage": 2955,
                "friendly_name": "home/attic/Fabian/Cube1_action",
                "icon": "mdi:gesture-double-tap"
            },
            "last_changed": "2021-01-09T21:19:01.347569+00:00",
            "last_updated": "2021-01-09T21:19:01.347569+00:00",
            "context": {
                "id": "208e5bd85569ef47eaed3454643ae366",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-09T21:19:01.347569+00:00",
    "context": {
        "id": "xyz",
        "parent_id": null,
        "user_id": null
    }
}

event name to listen to: state_changed
event field to use as sensor-state: new_state.attributes.side
event filter: entity_id: sensor.xyz_action

When I configure the sensor like this, I get "bad_state" as the state of the sensor. I guess the problem is that there are multiple sub-fields and that causes the problem. It works fine if there's only one sub-event.

Hi @Fabenissimo,

Sorry for the late answer, if not cited with the nick (@azogue) I don't get any notification :(

event name to listen to: state_changed

I bet you aren't using the Config Flow in the UI to set that sensor, because if you try you should get an error, am I right?

if event == EVENT_STATE_CHANGED:

return self.async_abort(reason="forbidden_event")

And the good reason to forbid that is because state_changed is a special event, which is fired every time an entity gets changed... and an event sensor IS an entity :)
So, not a good idea to listen to these...

And, anyway... if you have state_changed events, you already have an entity!! why the event sensor??? If you want to extract something from the attributes of some entity (your sensor.xyz_action), take a look to the template sensors

sensor:
  - platform: template
    sensors:
      cube1_side:
        friendly_name: "Cube 1 - Side"
        value_template: "{{ state_attr('sensor.xyz_action', 'side') }}"