Hass.states.set api reference

Hi.
Is there an api reference for the hass.states.set python procedure.
I am trying to set a sensor using a python script but keep getting syntax errors. I am sure it’s something simple but seeing some documentation on it would be a great help.

I have a template sensor to hold a power total in kWh and am trying to set it with the following:

hass.states.set(output_id, {‘week_total’: “10.5”, ‘unit_of_measurement’: “kWh”, ‘friendly_name’: “Plug Weekly Total”})

(‘output_id’ is a variable entered as service call data when calling the script and defines the entity)

The sensor is defined in my configuration as:

# Sensor to read weekly power consumption of smart plug
  - platform: template
    sensors:
      smart_plug_weekly_total:
        friendly_name: "Plug Weekly Total"
        unit_of_measurement: kWh
        value_template: "{{ attribute('week_total') | float }}"

Any help greatly appreciated.

this is covered in the dev docs, also covered in the extensive api, which is directly linked in the python script documenation.

  1. attribute() is not a valid jinja method. here is the documents for templating. Friendly reminder: You can’t make up methods and expect them to work.

  2. value_template sets the state. If you’re trying to set the state, set the state. Not an attribute of the state object.

  3. What’s the point of even having the sensor template? It does nothing other than complicate your setup. Just use the python script and set the entity_id. hass.states.set will create any entity.

What @petro said.

The syntax is hass.states.set(entitiy_id, state, {dict of attributes}) , I don’ know if state is optional.

1 Like

Ahh, got it.
Many thanks guys :slight_smile:

hi, sorry, links are dead (I mean, ones that should show exact API, e.g.: haas object details), are there any new ones?

I’m also looking for this. The “See available methods” links on the following page are broken:

They just lead to the “Home Assistant Developer Docs” home page. And searching from that page for “hass.states.set” only yields tutorial links - not documentation of the method.