Analog output GPIO pins from HomeAssistant value

I need help with configuration

sensor:
  - platform: homeassistant
    name: "Momentary Energy"
    entity_id: sensor.momentary_active_import
  - platform: homeassistant
    name: "Energy last h"
    entity_id: sensor.effekt_1h

Those two values from sensors at HomeAssistnat should be output as analog signals from 2 pins at ESP 8266

sensor.momentary_active_import → GPIO5
sensor.effekt_1h → GPIO4

Values should be remapped 0 - 400 → 0 - 3.3v

Example.
sensor.effekt_1h = 200 then GPIO4 should show 1,65 Volt

Please format your code as explained in https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

This whole post of yours is confusing.

Let’s clear some things up first here…

Just to be clear, you do have both of these sensors working, the sensor data seems accurate and they are integrated into HA through a different esp board, right?

What exactly are these sensors your using and what are you trying to control or what are you trying to achieve by creating analog outputs? Also, to be clear you are wanting static voltage output and not PWM?

Yes both sensors are working and one is collected be another ESP and the second one is calculated by Home Assistant.

First one is momentary energy consumption in KW 0-400Kw (Industrial meter)
Second one is calculated value of last 60minutes consumption 0-400Kwh

I try to send those values to an PLC thats controlling the heating system via Analog input on the PLC.

The PLC does not have support for MQTT and Home Asistant cant act like TCP Modbus Slave thats why this seams to be only way.

If pure analog signal not working PWM should be working to with a small cap on signal to smoth the pulses?

I try to do something like this

output:
  - platform: esp8266_pwm
    pin: GPIO04
    id: sensor.momentary_active_import
    - calibrate_linear:
      - 0 -> 0.0
      - 400 -> 100.0

  - platform: esp8266_pwm
    pin: GPIO05
    id: sensor.effekt_1h
    - calibrate_linear:
      - 0 -> 0.0
      - 400 -> 100.0

sensor:
  - platform: homeassistant
    name: "Momentary Energy"
    entity_id: sensor.momentary_active_import
    id: sensor.momentary_active_import

  - platform: homeassistant
    name: "Energy last h"
    entity_id: sensor.effekt_1h
    id: sensor.effekt_1h
    

I doubt that even compiles.

output:
  - platform: esp8266_pwm
    pin: GPIO04
    id: output_momentary_active_import

sensor:
  - platform: homeassistant
    name: "Momentary Energy"
    entity_id: sensor.momentary_active_import
    id: sensor_momentary_active_import
    on_value:
      then:
        - output.set_level:
            id: output_momentary_active_import
            level: !lambda "return x/400;"

Be aware that if your receiving circuit wants pure analog value, pwm output doesn’t work.
But you can try, even different frequencies…
If it doesn’t work, then you need dac.

Have to change value to level then it works like expected!

Thanks for extreamly fast help!!!

    on_value:
      then:
        - output.set_level:
            id: output_momentary_active_import
            value: !lambda "return x/400;"
    on_value:
      then:
        - output.set_level:
            id: output_momentary_active_import
            level: !lambda "return x/330;"

Of course… Sorry for that.

Thats was an easy fix the compline told to change it, Everything works great Thanks Karosm!

Do not forget to handle the case when the imported HA sensor is unavailable.