How to manually set state/value of sensor?

States, state_attr, etc. always return a string, so you need to convert it to a number.

- data_template:
      entity_id: cover.front1
      current_position: '{{ state_attr("cover.front123", "current_position") | int }}
1 Like

Are you talking about inside the python script?

Still the same unfortunately. I wonder if the '{{ and }}' are still keeping it as text? Is there a different way I can assign the value?

I am passing the data to the script by assigning the attribute within the automation data template.

Yes, I get that, but are you talking about

Inside the python script or in the yaml?

If I check the results in Developer Tools/States I get

The State Attribute for Front 123 is 0 but Front1 is ‘0’

So the Front End GUI doesn’t recognise that the single blind is up or down.

blinds

Front Blinds/All Blinds is Front 123, South is Front1

Ok, what is setting that value? You don’t have a single automation posted that shows how you set the current position.

So it must be in your python script or outside what you posted. That’s all I’m trying to get out of you.

Did you write the python script? Is the python script setting the value or are you doing it in another automation?

Hi Petro,

It is RodPayne’s python script above How to manually set state/value of sensor?

Post #15.

That’s the source of your issues. That’s a poorly written script in regards to maintaining attribute types. It keeps everything a string which is the root cause of your issue.

Why aren’t you using a cover group instead?

cover:
  - platform: group
    name: front123
    entities:
      - cover.front1
      - cover.front2
      - cover.front3

You won’t even need an automation. You’ll just have a cover that controls all 3.

1 Like

I guess, for some of us, we need to take the long way round to get at the best solution. :rofl:

It has been a very circuitous path for these blinds involving RF transmitters, rolling codes and complicated supply of power. They finally work and the GUI was the last bit.

Thank you so much for the simple solution for the icing on the cake :slight_smile: :grinning:

1 Like

Lol, it happens to all of us. Don’t fret over it. Cover groups is pretty new, I think it’s less than a year old.

1 Like

@ rodpayne the set_state.py is a great help thankyou.

However, I have a small issue when running mine, giving a no attribute lower() error.

Here is my log:

2020-02-06 20:35:08 INFO (SyncWorker_5) [homeassistant.components.python_script] Executing set_state.py: {'state': '0.105.1', 'entity_id': ['sensor.last_known_version']}
2020-02-06 20:35:08 ERROR (SyncWorker_5) [homeassistant.components.python_script.set_state.py] Error executing script: 'list' object has no attribute 'lower'
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/components/python_script/__init__.py", line 196, in execute
    exec(compiled.code, restricted_globals)
  File "set_state.py", line 13, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/core.py", line 876, in get
    return self._states.get(entity_id.lower())
AttributeError: 'list' object has no attribute 'lower'

Here is my sensor definition:

sensor:
  - platform: template
    sensors:
      last_known_version:
        value_template: "0.105.1" 

As you can see, I’m running latest(ish) version of HA.

Are you able to assist at all please?

how exactly do you do that?

Via this automation:

- alias: 'Notify Home Assistant Has Been Updated'
  initial_state: 'on'
  trigger:
    platform: homeassistant
    event: start
  condition: 
      - condition: template
        value_template: "{{ states('sensor.current_version') | string != states('sensor.last_known_version') | string }}"
  action:
    - service: tts.google_say
      entity_id: media_player.kitchen
      data_template:
        message: "Home Assistant has been updated to version {{states('sensor.current_version')}}"
        cache: false
    - service: python_script.set_state
      entity_id: sensor.last_known_version
      data_template:
        state: "{{states('sensor.current_version')}}"

And did it work with previous versions of HA?

Actually today is the first time I tried it and I updated earlier this evening, so I’m not sure…

hm… error message points inside states.get function… strange!
it works fine on 0.104.2, haven’t updated to 0.105 yet (but will over the weekend)

that should work, did you use to have it configured like this:

    - service: python_script.set_state
      entity_id:
      - sensor.last_known_version
      data_template:
        state: "{{states('sensor.current_version')}}"

If so, that would make it a list. Which would propagate error in question all the way down in the hass.states.get() in line 13.

change the code to this and it should always work.

inputEntity = data.get('entity_id')
if inputEntity is None:
    logger.warning("===== entity_id is required if you want to set something.")
else:
    if isinstance(inputEntity, list) and len(inputEntity) >= 1:
        inputEntity = inputEntity[0]

    if isinstance(inputEntity, str) and inputEntity:
        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)
    else:
        logger.warning("===== entity_id is required if you want to set something.")

could you explain why it is a list?

and I think this makes sense

inputEntity = data.get('entity_id')
if isinstance(inputEntity, list) and len(inputEntity) >= 1:
    inputEntity = inputEntity[0]
    
if isinstance(inputEntity, str) and inputEntity:
    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)
else:
    logger.warning("===== entity_id is required if you want to set something.")

Seems like it is a list looking at the square brackets in the log. I will play with it tonight.

I’d also like to know why it’s being input as a list. :slight_smile: