Dummy sensor

I like to make a sensor with no value that i can use the hass.states.set from a script to set values. What kind of sensor can i use ?

The states in the state machine do not have to correspond to real entities. You can make up entity_id’s, as long as they have a domain and object id (i.e., domain.objec_id.) See here, especially this statement:

You can make up any combination of domain and object id, even overwriting existing states.

Thank you :slight_smile:

1 Like

Hi Thomas,

Can you send through an example of how you set the dummy sensor value vis a script please?

Here is a script I’ve used (python_scripts/set_state.py):

if 'entity_id' not in data:
    logger.warning("===== entity_id is required if you want to set something.")
else:
    data = data.copy()
    inputEntity = data.pop('entity_id')
    inputStateObject = hass.states.get(inputEntity)
    if inputStateObject:
        inputState = inputStateObject.state
        inputAttributesObject = inputStateObject.attributes.copy()
    else:
        inputState = 'unknown'
        inputAttributesObject = {}
    if 'state' in data:
        inputState = data.pop('state')
    logger.debug("===== new attrs: {}".format(data))
    inputAttributesObject.update(data)

    hass.states.set(inputEntity, inputState, inputAttributesObject)

You call it like this:

- service: python_script.set_state
  data:
    entity_id: sensor.blah
    state: blah blah
    abc: 123
    xyz: do re mi

This will result in an entity with an id of ‘sensor.blah’ whose state will be ‘blah blah’ and which will have two attributes – abc = 123, and xyz = ‘do re mi’.

You need to specify at least entity_id. If you specify state, it will set the state to that value. If you don’t specify state, then the state will not change (or be set to ‘unknown’ if the entity did not yet exist in the state machine.) All other parameters will be written as attributes.

2 Likes

Big thanks for that,

(I’m only noting this for others)
One part that’s missing in your instruction was to add in configuration.yaml:
python_script:

Maybe you could explain why set_state.py needs to sit in a dir. called python_scripts/ not python_script/ but all code refers to “python_script”??

Thanks again,

Rob.

Sorry, you’re right, I forgot to mention that. It is documented here.

Um, because you might have more than one? :slight_smile: It’s just the way someone wrote it I guess.

@pnbruckner

Can you please help me out why the below config does not work?
It does not pass the data in the attributes for state and data.

   - service: python_script.set_state
      data:
        entity_id: sensor.water
        state: "{{ states.input_text.energyid_water.state }}"
        date:  "{{ states.sensor.date_time_iso.state }}"

Two very basic problems. 1, the indentation is wrong. 2. you’re using data instead of data_template.

1 Like

Thanks stupid mistake.

Question, this sensor.water does not survive a hass reboot.
All sensor data is gone, I want to store the last meter value in that sensor.

Well then you probably need to use an input_text and maybe an input_number and save the values with an automation.

Sorry to resurrect this but I just came across it and wondered how if at all this is different from this:

which suggests this, also 18 lines but different :wink: and which I have used for some time to set the state of an existing sensor.

inputEntity = data.get('entity_id')
if inputEntity is None:
    logger.warning("===== entity_id is required if you want to set something.")
else:    
    inputStateObject = hass.states.get(inputEntity)
    inputState = inputStateObject.state
    inputAttributesObject = inputStateObject.attributes.copy()

    for item in data:
        newAttribute = data.get(item)
        logger.debug("===== item = {0}; value = {1}".format(item,newAttribute))
        if item == 'entity_id':
            continue            # already handled
        elif item == 'state':
            inputState = newAttribute
        else:
            inputAttributesObject[item] = newAttribute
        
    hass.states.set(inputEntity, inputState, inputAttributesObject)

@pnbruckner

Im trying to do something similar. I created and input_number and i want this value that it is set to (1-25) to feed back into a card

Using list-card and want to be able to set the max amount of rows based on the slider. The list-card config is looking for row_limit: ‘10’ or replace 10 with any number up to 25.

I tried replacing the ‘10’ with things like {{ state(‘input_number.rss_slider’) }} but no such luck. How can i retrieve the state value and use it?

Thanks

edit:I realize this can be done with a conditional card but its 25x the amount of code to handle it.

Sorry, not following you. An input_number is a real entity. You change it and use its value like any normal real entity. You do not need, nor should use use, that script to change it.

i must have not done a good job explaining myself. Not trying to change the state of it but rather use the state(the number its set to) as a variable in a card

Then I’m confused. Why can’t you just display the input_number entity like you would display any entity in the UI? I’m also confused because you asked on this topic which was about creating a “dummy” entity, which is not the case here. Sorry, but I think maybe you’re asking in the wrong place, or I’m just misunderstanding.

sorry i didnt look at the header. i read some responses here that were similar to what i was looking for

1 Like