Increasing a number in a device

Hi,

New to the forum, and rather new to HA.

I have a script:

alias: New Script
sequence:
    device_id: 160a64d7d3ef0c80508b18cf65929c7a
    domain: number
    entity_id: number.exhaust_fan_speed_normal_47265
    type: set_value
    value: 55
mode: single

That sets the entity to a fixed value. The entity is a r/w value in a device.

What is the syntax for increasing it by lets say 5 instead of writing a fixed value? I found a number of threads about input_number, but it does not seem to be applicable for numbers from devices, anyway, I can’t get it to work.

What I would like is something like this:

alias: New Script
sequence:
    device_id: 160a64d7d3ef0c80508b18cf65929c7a
    domain: number
    entity_id: number.exhaust_fan_speed_normal_47265
    type: set_value
    value: number.exhaust_fan_speed_normal_47265 + 5
mode: single

but I cannot figure out the syntax.

Sorry if this question has been asked and answered before, it is pretty basic, but I have looked and have not found anything.

br H

Here’s some things which might move you in the right direction…

Here’s some examples of mine I use to increment thermostat and music volume that you may be able to adapt.

In an automation

  action:
    - service: climate.set_temperature
      data_template:
        temperature: "{{ state_attr('climate.air_conditioner', 'temperature')+1 |float}}"
      entity_id: climate.air_conditioner 

In a script

  sequence:
  - service: media_player.volume_set
    entity_id: media_player.the_mothership
    data_template:
      volume_level: '{{ states.media_player.the_mothership.attributes.volume_level + 0.02 }}'

Is the device an ESPHome device?

You might be able to adapt the below. Check/test with Developer > Services tool. You can test your templates with Developer > Template tool

service: number.set_value
data:
  value: 42

The trouble seems to be that number.set_value does not seem to be templatable in ha.

1 Like

Thanks for your replies, cannot make it work though.

Right now the script looks looks like this, tried with some float and str conversion / cast as well, no matter what I did I got errors when executing it. After much fiddling it is possible to save it without complaints about the syntax, using the browser editor.

alias: New Script
sequence:
  - service: number.exhaust_fan_speed_normal_47265
    entity_id: number.exhaust_fan_speed_normal_47265
    data_template:
      number.exhaust_fan_speed_normal_47265: "{{ states.number.exhaust_fan_speed_normal_47265 + 10 }}"
mode: single

Logger: homeassistant.components.websocket_api.http.connection
Source: helpers/service.py:249
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 10:20:47 PM (6 occurrences)
Last logged: 10:30:09 PM

[139818483298656] Error rendering data template: TypeError: unsupported operand type(s) for +: ‘DomainStates’ and ‘int’
[139818483298656] Error rendering data template: TypeError: unsupported operand type(s) for +: ‘DomainStates’ and ‘float’
[139818483298656] Error rendering data template: TypeError: unsupported operand type(s) for +: ‘DomainStates’ and ‘str’
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 423, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 1950, in _render_with_context
return template.render(**kwargs)
File “/usr/local/lib/python3.10/site-packages/jinja2/environment.py”, line 1301, in render
self.environment.handle_exception()
File “/usr/local/lib/python3.10/site-packages/jinja2/environment.py”, line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File “”, line 1, in top-level template code
TypeError: unsupported operand type(s) for +: ‘DomainStates’ and ‘int’

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/service.py”, line 242, in async_prepare_call_from_config
render = template.render_complex(config[conf], variables)
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 140, in render_complex
return {
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 141, in
render_complex(key, variables, limited, parse_result): render_complex(
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 147, in render_complex
return value.async_render(variables, limited=limited, parse_result=parse_result)
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 425, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for +: ‘DomainStates’ and ‘int’

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py”, line 200, in handle_call_service
await hass.services.async_call(
File “/usr/src/homeassistant/homeassistant/core.py”, line 1755, in async_call
task.result()
File “/usr/src/homeassistant/homeassistant/core.py”, line 1792, in _execute_service
await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
File “/usr/src/homeassistant/homeassistant/components/script/init.py”, line 473, in _service_handler
await self.async_turn_on(variables=service.data, context=service.context)
File “/usr/src/homeassistant/homeassistant/components/script/init.py”, line 432, in async_turn_on
await coro
File “/usr/src/homeassistant/homeassistant/components/script/init.py”, line 462, in _async_run
return await self.script.async_run(script_vars, context)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 1524, in async_run
await asyncio.shield(run.async_run())
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 409, in async_run
await self._async_step(log_exceptions=False)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 453, in _async_step
self._handle_exception(
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 476, in _handle_exception
raise exception
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 451, in _async_step
await getattr(self, handler)()
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 657, in _async_call_service_step
params = service.async_prepare_call_from_config(
File “/usr/src/homeassistant/homeassistant/helpers/service.py”, line 249, in async_prepare_call_from_config
raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: TypeError: unsupported operand type(s) for +: ‘DomainStates’ and ‘int’

If it is a fan you should use this Fan - Home Assistant

It’s i Nibe F730 heatpump, with a nibegw converter from modbus to UDP on a ESP32 then sent to HA via UDP.

Is it set up as a climate entity? If so, services are provided for those too.

Don’t know if it is setup as a climate entity. It has 578 entities, I use 30-40 of them. Reading and writing data to the heatpump works fine if done manually from HA UI.

There is a rather new integration in HA for Nibe heatpumps, I use that one,

According to the docs, it sets up a climate entity. Use that. Nibe Heat Pump - Home Assistant

Well I don’t know how that would help me, or how to do that. Thank you anyway, I think I’ll write a UDP server instead in C, that’s faster for me.

Well because you can use the built in services like you should be.

I checked what was supported in the climate entity, and it does not have support to do what I need to do.

So if anyone has any clue about the original question I’d appreciate that, if not not thanks for your help

you could perhaps implement as an input_number and use increment/decrement.

I don’t quite understand the set-up, but if it’s an ESPHome device, can you just implement a button on the ESP which increments the number on the ESPHome side?

Then you do a press button action on the HA side.

The fan speed is not to be controlled by manual intervention, but by the temperature of the evaporator, evaporator cold → increase fan speed, evaporator warmer → decrease fan speed.

Controlling the fan speed manually is no problem what so ever.

How are you achieving the manual control?

By changing the slider for Exhaust Fan speed control.

Thank you guys for your support. I did write a UDP server that talks directly to the NibeGW on ESP32 (an ESPHome device)

It is possible to specify additional IP’s that the GW sends the data to besides HA.

As you can see in the picture above the fan speed is low (55%) during daytime, This is when the compressor is not working at all or at very low speed (due to expensive electrical power), and hence it does not need much air through the evaporator. As the electrical power becomes cheaper at night, the compressor rpm increases, and the evaporator temperature decreases. This is what my UDP server looks at, low evaporator temperature, and then it increases the fan speed. Seem to work fine, and it saves me money for electricity.

I’m sure this would have been possible to achieve in HA, but I’m way much more familiar with C and assembly programming than with the more “high level programming” in HA, so it is faster for me.

Anyway, thank you for the support .

1 Like